package com.saurshaz.testmvc.controllers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.codehaus.jackson.map.annotate.JsonSerialize;

import test.Attributes;
import test.Data;

import com.saurshaz.testmvc.tree.JsTreeNode;

@JsonSerialize()
public class MyCollectionJsonObject {

	StringBuilder data = new StringBuilder();
	List<JsTreeNode> nodesList = new ArrayList<JsTreeNode>();

	public List<JsTreeNode> getNodesList() {
		return nodesList;
	}

	public void setNodesList(List<JsTreeNode> nodesList) {
		this.nodesList = nodesList;
	}

	public MyCollectionJsonObject() {

	}

	public boolean isAvailable() {
		return true;
	}

	public String getText() {
		return toString();
	}

	@Override
	public String toString() {

		StringBuilder sbReturnValueString = new StringBuilder();
		int i = 0;

		// \"data\": { \"title\": \"00002 - TestThing 2\", \"type\": \"link\", \"attributes\": { \"href\": \"/Site/39069\",\"class\": \"TL\" } } } ] }]");
		sbReturnValueString.append("[");
		for (Iterator<JsTreeNode> jstItr = this.nodesList.iterator(); jstItr
				.hasNext();) {

			if (i > 0) {
				sbReturnValueString.append(",");
			}
			i++;

			sbReturnValueString = prepareStringOutput(sbReturnValueString,
					jstItr.next());

		}
		sbReturnValueString.append("]");
		return sbReturnValueString.toString();

	}

	private StringBuilder prepareStringOutput(StringBuilder nodeString,
			JsTreeNode jsTreeNode) {

		Data data = jsTreeNode.getData();
		Attributes attributes = jsTreeNode.getAttributes();
		String state = jsTreeNode.getState();
		List<JsTreeNode> children = jsTreeNode.getChildren();

		// data.append("[{ \"data\":
		// { \"title\": \"Sites\",
		// \"attributes\": { \"href\": \"#\", \"class\":\"TreeTitle\" } },
		// \"state\": \"open\",
		// \"children\": [ { \"data\": { \"title\":\"00001 - Test Thing\",
		// \"type\": \"link\", \"attributes\": { \"href\":\"/Site/39063\",
		// \"class\": \"TL\" } } },
		// {
		nodeString.append("{");
		nodeString.append("\"data\":");

		nodeString.append("{");
		nodeString.append("\"title\":");
		nodeString.append(data.getTitle());
		nodeString.append(",");

		nodeString.append("\"attributes\":");
		nodeString.append("{");
		nodeString.append("\"href\":");
		nodeString.append(attributes.getRel());
		nodeString.append(",");
		nodeString.append("\"class\":");
		nodeString.append(attributes.getMdata());
		nodeString.append("}");

		nodeString.append("}");
		nodeString.append(",");

		nodeString.append("\"state\":");
		nodeString.append(state);
		nodeString.append(",");

		nodeString.append("\"children\":");

		nodeString.append("[");

		int j = 0;
		for (Iterator<JsTreeNode> itr = children.iterator(); itr.hasNext();) {
			JsTreeNode jsTreeChildNode = itr.next();
			if(j>0){
				nodeString.append(",");
			}
			prepareStringOutput(nodeString, jsTreeChildNode);
			j++;
		}

		nodeString.append("]");
		nodeString.append("}");

		return nodeString;
	}

}
